﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Coolite.Ext.Web;
using Newtonsoft.Json.Linq;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class CEOList : BasePage<CEOListViewModel>, IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Ext.IsAjaxRequest)
            {
                var boardActions = (from actionType in this.ViewModel.GetAvailableActions()
                                    select new
                                    {
                                        DecisionID = Enums.ToString(actionType),
                                        DecisionName = Enums.ToString(actionType, 2)
                                    });
                storeDecisions.DataSource = boardActions;
                storeDecisions.DataBind();
                storeBoards.DataSource = this.ViewModel.GetCEOBoards();
                storeBoards.DataBind();
            }
        }

        /// <summary>
        /// Handles the Selected event of the Board control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void Board_Selected(object sender, AjaxEventArgs e)
        {

            RowSelectionModel selecitonModel = grvBoards.SelectionModel.Primary as RowSelectionModel;
            if (selecitonModel.SelectedRows.Count > 0)
            {
                Guid boardID = selecitonModel.SelectedRows[0].RecordID.ToGuid();
                storeBoardLoans.DataSource = this.ViewModel.GetBoardLoans(boardID);
                storeBoardLoans.DataBind();
            }
            else
            {
                storeBoardLoans.RemoveAll();
            }
        }

        /// <summary>
        /// Handles the Clicked event of the BtnSubmit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnSubmit_Clicked(object sender, AjaxEventArgs e)
        {
            string jsonString = e.ExtraParams["grvBoardsGrid"];
            JArray jsonArray = JSON.Deserialize(jsonString) as JArray;

            List<Guid> boardsIDs = new List<Guid>();
            List<Guid> boardsDecisions = new List<Guid>();
            List<string> notesList = new List<string>();

            for (int i = 0; i < jsonArray.Count; i++)
            {
                if (!jsonArray[i]["DecisionID"].ToString().Trim('"').IsNullOrEmpty())
                {
                    boardsIDs.Add(jsonArray[i]["BoardID"].ToString().Trim('"').ToGuid());
                    boardsDecisions.Add(jsonArray[i]["DecisionID"].ToString().Trim('"').ToGuid());
                    notesList.Add(jsonArray[i]["Notes"].ToString().Trim('"'));
                }
            }

            this.ViewModel.SubmitBoards(boardsIDs, boardsDecisions, notesList);

            storeBoards.DataSource = this.ViewModel.GetCEOBoards();
            storeBoards.DataBind();
            RowSelectionModel rowSelecModl = grvBoards.SelectionModel.Primary as RowSelectionModel;
            rowSelecModl.ClearSelections();
        }

        /// <summary>
        /// Handles the Clicked event of the BtnCancle control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnCancle_Clicked(object sender, AjaxEventArgs e)
        {
            this.RedirectToPreviousPage();
        }


        #region IPermittedPage Members

        /// <summary>
        /// Gets the name of the permission.
        /// </summary>
        /// <value>
        /// The name of the permission.
        /// </value>
        public string PermissionName
        {
            get { return "CEOWaitingList"; }
        }

        #endregion
    }
}
